package sk.dudas.appengine.alicasperk.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;
import sk.dudas.appengine.alicasperk.service.DefaultServiceConfig;
import sk.dudas.appengine.alicasperk.web.controller.ControllerConfig;

/**
 * Created by IntelliJ IDEA.
 * User: oli
 * Date: 12.2.2012
 * Time: 13:26
 * To change this template use File | Settings | File Templates.
 */
@EnableWebMvc
@Configuration
@Import({ControllerConfig.class, DefaultServiceConfig.class})
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public TilesConfigurer getTilesConfigurer() {
        TilesConfigurer configurer = new TilesConfigurer();
        String[] definitions = {"/WEB-INF/tiles-defs/templates.xml"};
        configurer.setDefinitions(definitions);
        return configurer;
    }

    @Bean
    public UrlBasedViewResolver getTilesViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setOrder(0);
        resolver.setViewClass(TilesView.class);
        return resolver;
    }

    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        String[] basenames = {"messages", "version"};
        messageSource.setBasenames(basenames);
        return messageSource;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
